%% Excercise 1:
% Given the following dataset, which contains the prime numbers less than 100.
Dataset2 = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97];

% 4.1) Compute the sample median.
% 4.2) Compute the lower and upper quartiles.

%% Excercise 2:
% Given the following dataset, which contains Fibonacci numbers less than 100.
Dataset3 = [0,1,1,2,3,5,8,13,21,34,55,89];

% 5.1) Compute the standard deviation.
% 5.2) Compute the MAD(mean absolute deviation).

%% Excercise 3: load poiss.mat. 
% Let the discrete random variable X be the number of customers visiting
% ABC Bank in an hour. We know that X ~ Poiss(lambda). 
% The poiss.mat stores such information: 1000 records. 
% 2.1 Estimate lambda;
% 2.2 Compute P(X=k), k = minK, minK+1, minK+2, ..., maxK, where minK and maxK
% are the minimum and maximum of X, respectively;
% 2.3 Estimate P(X=k) by using the frequencies. 
% 2.4 Plot 2D figure with both results from 1.2 and 1.3: two curves;
clear;
clc;

load poiss.mat;

lambda = sum(X)/numel(X);
minK = min(X);
maxK = max(X);

k = minK:1:maxK;

p = zeros(1, numel(k));
for i = 1:numel(k)
   p(i) = lambda^k(i)/factorial(k(i)) * exp(-lambda);
end

f = zeros(1, numel(k));
[a,b] = histc(X,unique(X));
y = a(b);
for i = 1:numel(k)
    count = 0;
   for j = 1:numel(X)
       if k(i) == X(j)
           count = count + 1;
       end
   end
   f(i) = count/sum(X);
end

figure
plot(p)
hold on
plot(f)

%% Excercise 4: load norm.mat. 
% Let the continuous random variable X be the price changes of the product XYZ. 
% Positve values mean its price is increased and negative values mean its
% price is decreased. 
% We know that X ~ N(mu, sigma^2). 
% The norm.mat stores 1000 records of such information. 
% Please load the data and 
% 3.1) Estimate mu and sigma;
% 3.2) Compute P(X=a), a belongs to [minX, maxX] where minX and maxX are
% the minimum and maximum of X, respectively. 
% 3.3) Plot a 2D figure.
clear;
clc;

load norm.mat;

mu = mean(X);
sigma = std(X);

minX = min(X);
maxX = max(X);

p_max = (1/(sigma*sqrt(2*pi)))*exp(-(maxX - mu)^2/2*sigma^2);
p_min = (1/(sigma*sqrt(2*pi)))*exp(-(minX - mu)^2/2*sigma^2);

norm = normpdf(minX:maxX,mu,sigma^2);
figure;
plot(minX:maxX,norm)
hold on;
plot(minX,p_min,'r*');
hold on;
plot(maxX,p_max,'r*');